package com.gsc.funnycode.ctrl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.gsc.funnycode.dto.req.moneySearchReq;
import com.gsc.funnycode.entity.AddMoney;
import com.gsc.funnycode.entity.User;
import com.gsc.funnycode.mapper.AddMoneyMapper;
import com.gsc.funnycode.mapper.UserMapper;
import com.gsc.funnycode.service.AddMoneyService;
import com.gsc.funnycode.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: Orange
 * @Date: 15:18 2022/5/1
 */
@Controller
@Slf4j
public class MoneyController {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AddMoneyService addMoneyService;
    @Autowired
    AddMoneyMapper addMoneyMapper;

    @PostMapping("/search")
    public String search(
            String type,
            @DateTimeFormat(pattern = "yyyy-MM-dd")Date dateStart,
            @DateTimeFormat(pattern = "yyyy-MM-dd")Date dateEnd,
            HttpServletRequest request,
            Model model
    ){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        QueryWrapper<AddMoney> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",user.getUserId());
        if (StringUtils.isNotEmpty(type)){
            wrapper.eq("type",type);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (ObjectUtils.isNotEmpty(dateStart)){
            wrapper.apply("DATE_FORMAT(date,'%Y-%m-%d') >= {0}",format.format(dateStart));
        }
        if (ObjectUtils.isNotEmpty(dateEnd)){
            wrapper.apply("DATE_FORMAT(date,'%Y-%m-%d') <= {0}",format.format(dateEnd));
        }
        List<AddMoney> addMonies = addMoneyMapper.selectList(wrapper);
        BigDecimal allMoney = BigDecimal.ZERO;
        boolean flag = false;
        if (CollectionUtil.isNotEmpty(addMonies)){
            for (AddMoney e : addMonies){
                if (e != null){
                    flag =true;
                    BigDecimal a = e.getMoney() == null ? BigDecimal.ZERO : e.getMoney();
                    allMoney = allMoney.add(a);
                }
            }
        }
        QueryWrapper<AddMoney> wrapper2 = new QueryWrapper<>();
        wrapper.select("type").groupBy("type");
        List<AddMoney> types = addMoneyService.list(wrapper2);
        model.addAttribute("types",types);
        model.addAttribute("have",user.getAllMoney());
        model.addAttribute("allMoney",allMoney);
        if (flag){
            model.addAttribute("allMonies",addMonies);
        }
        return "index/money";
    }

    @GetMapping("/toM")
    public String toMoney(Model model,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<AddMoney> addMonies = addMoneyMapper.getListByUid(user.getAccount());
        BigDecimal allMoney = BigDecimal.ZERO;
        boolean flag = false;
        if (CollectionUtil.isNotEmpty(addMonies)){
            for (AddMoney e : addMonies){
                if (e != null){
                    flag =true;
                    BigDecimal a = e.getMoney() == null ? BigDecimal.ZERO : e.getMoney();
                    allMoney = allMoney.add(a);
                }
            }
        }
        QueryWrapper<AddMoney> wrapper = new QueryWrapper<>();
        wrapper.select("type").groupBy("type");
        List<AddMoney> types = addMoneyService.list(wrapper);
        model.addAttribute("types",types);
        model.addAttribute("have",user.getAllMoney());
        model.addAttribute("allMoney",allMoney);
        if (flag){
            model.addAttribute("allMonies",addMonies);
        }
        return "index/money";
    }

    @GetMapping("/toL")
    public String toL(Model model){
        List<String> type = addMoneyMapper.getTypes();
        model.addAttribute("type",type);
        return "index/logMoney";
    }
    @PostMapping("/add")
    public String add(BigDecimal money, @DateTimeFormat(pattern = "yyyy-MM-dd") Date date, String type, String reason, HttpServletRequest request, Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        AddMoney addMoney = new AddMoney(null,reason,money,type,date,user.getUserId());
        if (addMoneyService.save(addMoney)){
            return toMoney(model,request);
        }
        model.addAttribute("msg","记账失败，系统出错");
        return "index/logMoney";
    }

    @GetMapping("/del/{id}")
    public String del(@PathVariable(value = "id") String id, Model model, HttpServletRequest request){
        addMoneyService.remove(new QueryWrapper<AddMoney>().eq("id",id));
        return toMoney(model,request);
    }

}
